package com.opo5.logisticsmanagement.service.impl;

import com.opo5.logisticsmanagement.common.MD5Utils;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.LoginDao;
import com.opo5.logisticsmanagement.domain.User;
import com.opo5.logisticsmanagement.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class loginServiceImpl implements loginService {

    @Autowired
    private LoginDao loginDao;

    @Override
    public R<Object> login(User user) {
        R<Object> listR = new R<>();
        User u = loginDao.findByNickname(user);
        if (u != null){
//            System.out.println(u.getNickname()+"---"+u.getPassword());
            if (user.getPassword()!=null && u.getPassword().equals(MD5Utils.encrypt(user.getPassword(),MD5Utils.MD5_KEY))){
                listR.setCode(200);
                listR.setMsg("success");
                Map<Object,Object> mp = new HashMap<>();
                mp.put("user_info",loginDao.findByNickName(user));
                mp.put("user_role",loginDao.findRole(user));
                listR.setData(mp);
                listR.setMap(mp);
            }
            else {
                listR.setCode(400);
                listR.setMsg("error on pos02");
            }
        }else {
            listR.setCode(400);
            listR.setMsg("error on pos01");
        }
        return listR;
    }
}
